package cn.calm.repaymentNotice.client;

import java.util.Calendar;

import cn.calm.repaymentNotice.R;
import cn.calm.repaymentNotice.dao.DBService;
import cn.calm.repaymentNotice.entity.CreditPlanRecord;
import cn.calm.repaymentNotice.entity.RepaymentNotice;
import cn.calm.repaymentNotice.entity.RepaymentNoticeInfo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class CreditPlanActivity extends Activity {
	private RepaymentNotice notice;
	private EditText yearText;
	private EditText monthAmountText;
	private DBService dao;
	private CreditPlanRecord record;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		notice=intent.getParcelableExtra("info");
		if(notice.id==-1){
			record=new CreditPlanRecord();
		}else{
			try {
				RepaymentNoticeInfo info=getDbService().getRepaymentNoticeInfo(notice.id);
				record=getDbService().getCreditPlanRecord(info);
			} catch (RepaymentNoticeException e) {
				e.printStackTrace();
			}
		}
		setContentView(R.layout.activity_credit_plan);
		yearText=(EditText) findViewById(R.id.year);
		if(notice.year>0){
			yearText.setText(notice.year+"");
		}
		monthAmountText=(EditText) findViewById(R.id.month);
		monthAmountText.setText(String.valueOf(record.predictRepaymentAmount));
		Button pass = (Button) findViewById(R.id.pass);
		pass.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				try {
					validate();
					
					Calendar today=Calendar.getInstance();
					int day = today.get(Calendar.DAY_OF_MONTH);
					if(day > notice.repaymentDate){
						today.add(Calendar.MONTH, 1);
					}
					today.set(Calendar.DAY_OF_MONTH, notice.repaymentDate);
					record.predictRepaymentDate=today.getTime();
					if(notice.id == -1){
						getDbService().saveRepaymentNotice(notice);
						record.repaymentNoticeId = notice.id;
						int months=notice.year*12;
						
						for(int i=0;i<months;i++){
							getDbService().saveCreditPlanRecord(record,null);
							today.add(Calendar.MONTH, 1);
							record.predictRepaymentDate=today.getTime();
						}
					}else{
						RepaymentNoticeInfo tempInfo=getDbService().getRepaymentNoticeInfo(notice.id);
						getDbService().saveRepaymentNotice(notice);
						record.repaymentNoticeId = notice.id;
						getDbService().saveCreditPlanRecord(record,tempInfo.predictRepaymentDate);
					}
					
					Intent intent=new Intent();
					intent.setClass(CreditPlanActivity.this, MainActivity.class);
					startActivity(intent);
				} catch (RepaymentNoticeException e) {
					Toast.makeText(getApplicationContext(),e.getMessage(),
							Toast.LENGTH_LONG).show();
				}
			}
		});
	}
	
	protected void validate() throws RepaymentNoticeException {
		StringBuilder strBu=new StringBuilder();
		String yearStr=yearText.getText().toString().trim();
		String monthStr=monthAmountText.getText().toString().trim();
		
		if(yearStr.length()<=0){
			strBu.append("请输入贷款年限!\n");
		}
		if(monthStr.length()<=0){
			strBu.append("请输入月供金额!\n");
		}
		if(strBu.length()>0){
			throw new RepaymentNoticeException(strBu.deleteCharAt(strBu.length()-1).toString());
		}
		int year=Integer.valueOf(yearStr);
		if(year<1){
			strBu.append("贷款年限请输入大于1的数字!\n");
		}
		double monthAmount=Double.valueOf(monthStr);
		if(monthAmount<=0){
			strBu.append("月供金额请输入大于0的数字!\n");
		}
		if(strBu.length()>0){
			throw new RepaymentNoticeException(strBu.deleteCharAt(strBu.length()-1).toString());
		}
		record.predictRepaymentAmount=monthAmount;
		notice.year=year;
		
		
	}
	
	private DBService getDbService(){
		if(dao == null)
		{
			dao=DBService.getInstance(getApplicationContext());
		}
		return dao;
	}

}
